package com.aliyun.heiyu.powermaster.server.strategy.analyzer;

import com.aliyun.heiyu.common.util.DateUtil;
import com.aliyun.heiyu.common.util.StringUtil;
import com.aliyun.heiyu.powermaster.proto.PowerStrategy;
import com.aliyun.heiyu.powermaster.proto.StrategyAction;
import com.aliyun.heiyu.powermaster.server.MasterConstant;
import com.aliyun.heiyu.powermaster.server.manager.ThunderManager;
import com.aliyun.heiyu.powermaster.server.manager.ThunderManagerImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;


public class CappingStrategyAnalyzer extends StrategyAnalyzer {
    private static Logger logger = LoggerFactory.getLogger(CappingStrategyAnalyzer.class);

    private ThunderManager thunderManager;

    public CappingStrategyAnalyzer() {
        super(MasterConstant.CAPPING, PowerStrategy.CAPPING_POWER.name());
        this.thunderManager = new ThunderManagerImpl();
    }

    /**capping动作的触发条件：实际监控值, cupUsage、power满足条件时触发*/
    @Override
    public Boolean monitorCondition(StrategyAction strategyAction, String sn, String level) {
        logger.debug("手动策略监控设备  == "+sn+"capping条件");
        Boolean status = thunderManager.getStatusOfCapping(sn);
        if(status == null){
            logger.warn("查询capping状态==null,sn = {}",sn);
            return null;
        }
        StrategyAction.ActionCondition condition = strategyAction.getActionCondition();
        //让capping动作优先级高于uncapping
        boolean b1 = false; //maxcpuusage
        boolean b2 = false; //maxpower
        boolean b3 = false; //mincpuusage
        boolean b4 = false; //minpower
        String cpuUsage = super.monitor.getCPUUsage(sn,0);

        if(!StringUtil.isEmpty(cpuUsage)){
            b1 = Double.parseDouble(cpuUsage) > Double.parseDouble(condition.getMaxCPUUsage());
            b3 = Double.parseDouble(cpuUsage) < Double.parseDouble(condition.getMinCPUUsage());
        }

        String power = super.monitor.getPower(sn,0);
        if(!StringUtil.isEmpty(power)){
            b2 = Double.parseDouble(power) > Double.parseDouble(condition.getMaxPower());
            b4 = Double.parseDouble(power) < Double.parseDouble(condition.getMinPower());
        }

        SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.HOURMINUTE_PATTERN);
        Timestamp stamp1 = DateUtil.string2Stamp(condition.getStartTime(), sdf);
        Timestamp stamp2 = DateUtil.string2Stamp(condition.getEndTime(), sdf);
        Time nowTime = new Time(System.currentTimeMillis());
        String format = sdf.format(nowTime);
        Timestamp nowStamp = DateUtil.string2Stamp(format, sdf);
        boolean b5 = (nowStamp.compareTo(stamp1) > 0) && (nowStamp.compareTo(stamp2) < 0);
        if(!b5){
            logger.debug("不在动作时间范围内L， capping 不进行动作");
            return null;
        }
        //capping
        if(b1 || b2){
            logger.debug("capping 进行动作");
            if(status == null || !status){
                return true;
            }
            return null;
        }
        //uncapping
        if(b3 || b4){
            logger.debug("uncapping 进行动作");
            if(status == null || status){
                return Boolean.FALSE;
            }
            return null;
        }
        return null;
    }

    @Override
    protected Boolean isTrue(String sn) {
        //todo 比较具体参数
        boolean b = thunderManager.getStatusOfCapping(sn);
        return b;
    }
}
